package com.example.hotel;

import java.io.InputStream;
import java.lang.reflect.Array;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.example.hotel.R;
import com.example.hotel.R.id;
import com.example.hotel.R.layout;

import android.R.color;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.AvoidXfermode;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Info extends Activity implements OnItemClickListener,
		OnClickListener {

	int indext = 3;
	int i = 0;
	int i0 = 0;
	int i1 = 1;
	int i2 = 2;
	int tmp = 0;
	Bitmap[] data = null;
	ArrayList<String> image_info = new ArrayList<String>();
	ArrayList<String> name_info = new ArrayList<String>();
	ArrayList<String> detail = new ArrayList<String>();
	ListView list_menu, l1;
	TextView txt_room, txt_name, wellcome, info_list, txt_name1;
	ImageView image_room, back_home, imageback, image_cart, image_msg,
			image_bill;
	ArrayList<HashMap<String, String>> myArrList;
	List<String> list_info;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info);
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		TextView tvAmount = (TextView) findViewById(R.id.tvAmount);
		tvAmount.setText("("
				+ ((Global) getApplicationContext()).cart_count.toString()
				+ ")");
		getInfo_list();

		image_room = (ImageView) findViewById(R.id.image_room);
		back_home = (ImageView) findViewById(R.id.imagehome);
		imageback = (ImageView) findViewById(R.id.imageback);
		image_cart = (ImageView) findViewById(R.id.image_cart);
		image_msg = (ImageView) findViewById(R.id.image_msg);
		image_bill = (ImageView) findViewById(R.id.image_bill);
		list_menu = (ListView) findViewById(R.id.list_menu);

		list_menu.setDivider(null);
		txt_room = (TextView) findViewById(R.id.txt_room);
		txt_name = (TextView) findViewById(R.id.txt_name);
		wellcome = (TextView) findViewById(R.id.tvWellcome);
		info_list = (TextView) findViewById(R.id.info_list);
		wellcome.setText(((Global) getApplicationContext()).wellcome);
		String tmp = String.valueOf(image_info.get(0));
		Bitmap temp = getBitmapFromURL(tmp);
		
		/*for (int i = 0; i < image_info.size(); i++) {
			String tmp = String.valueOf(image_info.get(i));
			Bitmap temp = getBitmapFromURL(tmp);
			if (temp != null) {
				data[i] = temp;
			}
		}*/
		image_room.setImageBitmap(temp);
		txt_room.setText(Html.fromHtml(detail.get(0)));
		txt_name.setText(name_info.get(0));
		// ArrayAdapter<String> list = new ArrayAdapter<String>(this,
		// android.R.layout.test_list_item, name_info);

		/*---------SetCustomList-------*/
		ListAdapter listAdapter;
		listAdapter = new CustomListAdapter(Info.this, R.layout.info_list,
				name_info);
		list_menu.setAdapter(listAdapter);
		/*-----------------------------*/
		// list_menu.setAdapter(list);
		// test_list_item
		list_menu.setOnItemClickListener(this);

		/*------setfonts------*/

		Typeface Font = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-Kittithada.ttf");
		Typeface FontBold = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-KittithadaBold.ttf");
		TextView tv_amount = (TextView) findViewById(R.id.tvAmount);
		txt_room.setTypeface(Font);
		txt_name.setTypeface(FontBold);
		tv_amount.setTypeface(Font);		
		wellcome.setTypeface(FontBold);

		/*--------------------*/
		image_bill.setOnClickListener(this);
		image_cart.setOnClickListener(this);
		image_msg.setOnClickListener(this);
		imageback.setOnClickListener(this);
		back_home.setOnClickListener(this);
		Boolean Switch_page = ((Global)getApplicationContext()).Switch_page;
		if(Switch_page)
		{
			((Global) getApplicationContext()).callpage1 = com.example.hotel.Hotel_info.class;
		}
		else
		{
			((Global) getApplicationContext()).callpage1 = com.example.hotel.Local_attraction.class;
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imagehome:
			startActivity(new Intent(Info.this, home.class));
			break;
		case R.id.imageback:
			Class myClass = ((Global) getApplicationContext()).callpage1;
			startActivity(new Intent(Info.this, myClass));
			break;
		case R.id.image_cart:
			((Global) getApplicationContext()).callpage1 = com.example.hotel.Info.class;
			((Global) getApplicationContext()).nameCart = "show_cart";
			startActivity(new Intent(Info.this, Add_cart.class));
			break;
		case R.id.image_msg:
			((Global) getApplicationContext()).callpage1 = com.example.hotel.Info.class;
			startActivity(new Intent(Info.this, Message_list.class));
			break;
		case R.id.image_bill:
			((Global) getApplicationContext()).callpage1 = com.example.hotel.Info.class;
			startActivity(new Intent(Info.this, Billing_history.class));
			break;
		}
	}

	private Bitmap getBitmapFromURL(String tmp) {
		// TODO Auto-generated method stub
		URL url = null;
		Bitmap myBitmap = null;
		try {

			url = new URL(tmp);
			InputStream input = url.openConnection().getInputStream();

			BitmapFactory.Options options = new BitmapFactory.Options();

			myBitmap = BitmapFactory.decodeStream(input, null, options);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return myBitmap;

	}

	public void onItemClick(AdapterView<?> list, View arg1, int position,
			long arg3) {
		// TODO Auto-generated method stub
		if (list.getChildAt(position) != null) {

			if (position != tmp) {

				list.getChildAt(tmp).setBackgroundColor(
						Color.alpha(color.white));

				list.getChildAt(position).setBackgroundColor(Color.CYAN);
				String tmp1 = String.valueOf(image_info.get(position));
				Bitmap temp = getBitmapFromURL(tmp1);			
				image_room.setImageBitmap(temp);
				txt_name.setText(name_info.get(position));
				txt_room.setText(Html.fromHtml(detail.get(position)));
				tmp = position;
				String a = String.valueOf(position);
				// Toast.makeText(this, a, Toast.LENGTH_LONG).show();
			} else if (tmp == 0) {
				list.getChildAt(tmp).setBackgroundColor(Color.CYAN);
				String tmp1 = String.valueOf(image_info.get(position));
				Bitmap temp = getBitmapFromURL(tmp1);			
				image_room.setImageBitmap(temp);
				txt_name.setText(name_info.get(position));
				txt_room.setText(Html.fromHtml(detail.get(tmp)));
				tmp = position;
				String a = String.valueOf(position);
				// Toast.makeText(this, a, Toast.LENGTH_LONG).show();
			} else {
				list.getChildAt(tmp).setBackgroundColor(
						Color.alpha(color.white));

			}
		}

	}

	public void getInfo_list() {
		String Servicesname = ((Global) getApplicationContext()).Servicesname;
		String URL1 = ((Global) getApplicationContext()).url;
		String SOAP_ACTION = ((Global) getApplicationContext()).soap_acction
				+ Servicesname;
		String NAMESPACE = "";
		String METHOD_NAME = Servicesname;
		String lang = ((Global) getApplicationContext()).lang;
		String id_image_info = ((Global) getApplicationContext()).id_imageInfo;
		String str = "";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapObject responses = null;
		request.addProperty("id", id_image_info);
		request.addProperty("lang", lang);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
		androidHttpTransport.debug = true;

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			responses = (SoapObject) envelope.bodyIn;
			str = responses.toString();
			int indexSub = str.indexOf("=");
			String b = str.substring(indexSub + 1);
			String[] a = b.split("\\|");
			indexSub = a[a.length - 1].indexOf("}");
			b = a[a.length - 1].substring(0, indexSub - 2);
			a[a.length - 1] = b;
			for (int i = 0; i < a.length; i++) {
				String[] tmp = a[i].split("\\*");
				name_info.add(tmp[0]);
				image_info.add(tmp[1]);
				detail.add(tmp[2]);

			}

		} catch (Exception e) {
			Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();

		}
	}

	// /CustomList setFontListView
	@SuppressWarnings("rawtypes")
	private class CustomListAdapter extends ArrayAdapter {

		private Context mContext;
		private int id;
		private List<String> items;

		@SuppressWarnings("unchecked")
		public CustomListAdapter(Context context, int textViewResourceId,
				List<String> list) {
			super(context, textViewResourceId, list);
			mContext = context;
			id = textViewResourceId;
			items = list;
		}

		@Override
		public View getView(int position, View v, ViewGroup parent) {
			View mView = v;
			if (mView == null) {
				LayoutInflater vi = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				mView = vi.inflate(id, null);
			}
			TextView text = (TextView) mView.findViewById(R.id.info_list);
			if (items.get(position) != null) {
				Typeface FontBold = Typeface.createFromAsset(getAssets(),
						"fonts/PSL-KittithadaBold.ttf");
				text.setTypeface(FontBold);
				text.setText(items.get(position));
			}
			return mView;
		}

	}

}
